package org.itboys.baseobject.util;

import java.util.List;

import org.itboys.baseobject.dto.Point;

public class PointUitls {

	/**
	 * 判断 1个 坐标是否在一个多边形内
	 * @param points n个坐标的多边形
	 * @param lng 经度
	 * @param lat 纬度
	 * @return
	 */
	public boolean isInPolygon(List<Point> points, double lng, double lat) {
		int polySides = points.size();
		int i,j = polySides - 1;
		boolean oddNodes = false;
		for (i = 0; i < polySides; i++) {
			if((points.get(i).getLat() < lat && points.get(j).getLat() >= lat
				|| points.get(j).getLat() < lat && points.get(i).getLat() >= lat)
					&& (points.get(i).getLng() <= lng || points.get(j).getLng() <= lng)) {
				oddNodes ^= (points.get(i).getLng() + (lat-points.get(i).getLat()) / (points.get(j).getLat() - points.get(i).getLat())*(points.get(j).getLng()-points.get(i).getLng())<lng);
			}
			j=i;
		}
		return oddNodes;
	}
}
